<?php

if (empty($args)) {
    return;
}

$category_and_posts = $args["category_and_posts"];

if (empty($category_and_posts)) {
    return;
}
$widget_title = $args["title"];
$cat_id = $category_and_posts[0]['category'];
$post_ids = $category_and_posts[0]['posts'];
$post_ids = explode(',', $post_ids);

// 查询
$query_args = array(
    'ignore_sticky_posts' => false,
    'post_status'         => 'publish',
    'post__in' => $post_ids,
    'posts_per_page'      => 8,
);

$PostData = new WP_Query($query_args);


?>

    <div class="bg-white w-full">
        <section class="container">
            <div class="section-title text-center mb-4">
                <h3 class="py-4"><?php echo $widget_title;?></h3>
                <p class="text-muted mb-0 d-flex gap-2">
                    <?php foreach($category_and_posts as $index => $item): ?>
                        <button type="button" class="btn btn-outline-dark home-cat-btn <?php if($index == 0){ echo "active"; } ?>" id="cat<?php echo $index;?>"><?php echo get_category($item['category'])->name;?></button>
                    <?php endforeach;?>
                </p>
            </div>


            <div class="cms-post-warp row g-2 g-md-3">

                <?php if ( $PostData->have_posts() ):?>
                    <div class="row row-cols-2 row-cols-md-3 row-cols-lg-4 g-2 g-md-3 g-lg-4" id="ajax-posts-row">
                        <?php if ($PostData->have_posts()):
                            while ($PostData->have_posts()): $PostData->the_post();
                                get_template_part('template-parts/loop/item', '');
                            endwhile;
                        else:
                            get_template_part('template-parts/loop/item', 'none');
                        endif;?>
                    </div>
                <?php endif;?>
            </div>

        </section>

    </div>

<?php wp_reset_postdata();?>